//
//  CarOwnerOrdersViewController.m
//  UcarTest
//
//  Created by 陈曦 on 16/3/13.
//  Copyright © 2016年 chenxi. All rights reserved.
//

#import "CarOwnerOrdersViewController.h"
#import "Define.h"
#import "MainPageViewController.h"
#import "SegmentTapView.h"
#import "FlipTableView.h"
#import "FirstCarOwnerViewController.h"
#import "SecondCarOwnerViewController.h"
#import "ThirdCarOwnerViewController.h"
#import "ForthCarOwnerViewController.h"
#import "CarOwnerOrdersDetailsViewController.h"
#import "RefuseViewController.h"

#import "AFNetworking.h"
#import "NetRequestGroup.h"
#import "Access_token.h"
#import "MBProgressHUD.h"

@interface CarOwnerOrdersViewController ()<SegmentTapViewDelegate,FlipTableViewDelegate>
@property (nonatomic, strong)SegmentTapView *segment;

@property (nonatomic, strong)FlipTableView *flipView;

@property (strong, nonatomic) NSMutableArray *controllsArray;


@end

@implementation CarOwnerOrdersViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    //导航条
    [self navigationItemSet];
    
    //添加选择条
    [self initSegment];

    [self initFlipTableView];

}

#pragma mark segment选择控制器
-(void)initSegment{
    self.segment = [[SegmentTapView alloc] initWithFrame:CGRectMake(0, 64, kScreenWidth, 45) withDataArray:[NSArray arrayWithObjects:@"全部",@"待确认",@"用车",@"已完成", nil] withFont:15];
    self.segment.delegate = self;
    [self.view addSubview:self.segment];
}


-(void)initFlipTableView{
    
    if (!self.controllsArray) {
        
        self.controllsArray = [[NSMutableArray alloc] init];
        
    }
    
    //1-------------------------------------------------------------------
    FirstCarOwnerViewController *v1 = [[FirstCarOwnerViewController alloc] init];
    
    v1.refuseBlock = ^(NSString *orderID) {
        
        [self pushToRefuseVController:orderID];
        
    };
    
    v1.pushBlock = ^(NSString *orderID, NSString *order_status) {
        
        [self pushToCarOwnerOrdersDetailsViewController:orderID];
        
    };
    
    v1.defineBlock = ^(NSString *orderID) {
        
        [self NetworkRequests:orderID];
        
    };

    //2-------------------------------------------------------------------
    SecondCarOwnerViewController *v2 = [[SecondCarOwnerViewController alloc] init];
 
    v2.refuseBlockTwo = ^(NSString *orderID) {
        
        [self pushToRefuseVController:orderID];
        
    };
    
    v2.pushBlockTwo  = ^(NSString *orderID, NSString *order_status) {
        
        [self pushToCarOwnerOrdersDetailsViewController:orderID];
        
    };
    
    v2.defineBlockTwo = ^(NSString *orderID) {
        
        [self NetworkRequests:orderID];
        
    };

    //3-------------------------------------------------------------------
    ThirdCarOwnerViewController *v3 = [[ThirdCarOwnerViewController alloc] init];
    
    v3.pushBlockThree  = ^(NSString *orderID, NSString *order_status) {
        
        [self pushToCarOwnerOrdersDetailsViewController:orderID];
        
    };

    
    //4-------------------------------------------------------------------
    ForthCarOwnerViewController *v4 = [[ForthCarOwnerViewController alloc] init];

    v4.pushBlockFour  = ^(NSString *orderID, NSString *order_status) {
        
        [self pushToCarOwnerOrdersDetailsViewController:orderID];
        
    };

    
    [self.controllsArray addObject:v1];
    [self.controllsArray addObject:v2];
    [self.controllsArray addObject:v3];
    [self.controllsArray addObject:v4];
    
    self.flipView = [[FlipTableView alloc] initWithFrame:CGRectMake(0, 110, kScreenWidth, self.view.frame.size.height - 110) withArray:_controllsArray];
    self.flipView.delegate = self;
    [self.view addSubview:self.flipView];
}
#pragma mark -------- select Index
-(void)selectedIndex:(NSInteger)index
{
    [self.flipView selectIndex:index];
    
}
-(void)scrollChangeToIndex:(NSInteger)index
{
    [self.segment selectIndex:index];
}





//调到租客订单详情界面
- (void) pushToCarOwnerOrdersDetailsViewController:(NSString *) orderSnID{
    
    CarOwnerOrdersDetailsViewController *carOwnerVC = [[CarOwnerOrdersDetailsViewController alloc] init];
    carOwnerVC.id = orderSnID;
    
    [self.navigationController pushViewController:carOwnerVC animated:nil];
    
}

//跳到拒绝订单详情界面
- (void) pushToRefuseVController:(NSString *) orderSnID{
    
    RefuseViewController * RefuseVC = [[RefuseViewController alloc] init];
    
    RefuseVC.id = orderSnID;
    
    [self.navigationController pushViewController:RefuseVC animated:nil];
    
}



#pragma mark 网络请求
- (void) NetworkRequests:(NSString *)orderID{
    
    //初始化进度框，置于当前的View当中
    MBProgressHUD *HUD = [[MBProgressHUD alloc] initWithView:self.view];
    [self.view addSubview:HUD];
    HUD.label.text = @"正在加载";
    [HUD showAnimated:YES];//HUD开始
    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    //申明返回的结果是json类型
    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    //申明请求的数据是json类型
    manager.requestSerializer = [AFHTTPRequestSerializer serializer];
    //如果报接受类型不一致请替换一致text/html或别的
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
    
    Access_token *useAccess = [Access_token shareInstance];
    
    //传入的参数
    NSDictionary *para = @{
                           @"type" : @"1",
                           @"access_token" : useAccess.access_token,
                           @"audit_type" : @"0",
                           @"id" : orderID
                           };
    
    //你的接口地址
    NSString *url = [NSString stringWithFormat:@"%@%@", kRealmName, kNetOrderAudit];
    //发送请求
    [manager POST:url parameters:para success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        [HUD hideAnimated:YES];
        
        NSDictionary *dic = responseObject;
        
        self.status = dic[@"status"];
        
        self.msg = dic[@"msg"];
        
        
        [self resultAnalysis];
        
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        [HUD hideAnimated:YES];
        
        //初始化提示框；
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"没有网络" preferredStyle:  UIAlertControllerStyleAlert];
        
        [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            //点击按钮的响应事件；
        }]];
        
        //弹出提示框；
        [self presentViewController:alert animated:true completion:nil];
        
    }];
    
}


#pragma mark 返回值分析
- (void) resultAnalysis {
    
    int status = [self.status intValue];
    
    switch (status) {
        case 99:{
            //初始化提示框；
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:self.msg preferredStyle:  UIAlertControllerStyleAlert];
            
            [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                //点击按钮的响应事件；
            }]];
            
            //弹出提示框；
            [self presentViewController:alert animated:true completion:nil];
            
        }
            
            break;
        case 98:{
            
            //初始化提示框；
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:self.msg preferredStyle:  UIAlertControllerStyleAlert];
            
            [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                //点击按钮的响应事件；
            }]];
            
            //弹出提示框；
            [self presentViewController:alert animated:true completion:nil];
            
        }
            break;
        case 97:
        {
            //初始化提示框；
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:self.msg preferredStyle:  UIAlertControllerStyleAlert];
            
            [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                //点击按钮的响应事件；
            }]];
            
            //弹出提示框；
            [self presentViewController:alert animated:true completion:nil];
            
        }
            
            break;
        case 96:
        {
            
            //初始化提示框；
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:self.msg preferredStyle:  UIAlertControllerStyleAlert];
            
            [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                //点击按钮的响应事件；
            }]];
            
            //弹出提示框；
            [self presentViewController:alert animated:true completion:nil];
            
        }
            
            break;
        case 1:
        {
            //初始化提示框；
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:self.msg preferredStyle:  UIAlertControllerStyleAlert];
            
            [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                //点击按钮的响应事件；
            }]];
            
            //弹出提示框；
            [self presentViewController:alert animated:true completion:nil];
            
        }
            break;
        case 2:
        {
            //初始化提示框；
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:self.msg preferredStyle:  UIAlertControllerStyleAlert];
            
            [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                //点击按钮的响应事件；
            }]];
            
            //弹出提示框；
            [self presentViewController:alert animated:true completion:nil];
            
        }
            break;
            
        case 100:
        {
            
            //初始化提示框；
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"确认成功" preferredStyle:  UIAlertControllerStyleAlert];
            
            [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                //点击按钮的响应事件；
                
                CarOwnerOrdersViewController *carowner = [[CarOwnerOrdersViewController alloc] init];
                
                [self.navigationController pushViewController:carowner animated:nil];
                
            }]];
            
            //弹出提示框；
            [self presentViewController:alert animated:true completion:nil];
            
        }
            break;
            
        default:
            break;
    }
    
    
    
}


//导航条设置
- (void) navigationItemSet {
    self.navigationController.navigationBarHidden = NO;
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 44)];
    titleLabel.backgroundColor = [UIColor clearColor];
    titleLabel.textColor = [UIColor whiteColor];
    titleLabel.font = [UIFont boldSystemFontOfSize:18];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.text = @"车主订单";
    self.navigationItem.titleView = titleLabel;
    self.navigationController.navigationBar.barTintColor = mainColor;
    self.view.backgroundColor = backgroundMainColor;
    
    
    //重定义返回键
    UIButton *back = [UIButton buttonWithType:UIButtonTypeCustom];
    back.frame = CGRectMake(0, 0, 22, 22);
    [back setImage:[UIImage imageNamed:@"iconfont-fanhui-white"] forState:UIControlStateNormal];
    [back addTarget:self action:@selector(backTpMainView) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *backBar = [[UIBarButtonItem alloc] initWithCustomView:back];
    self.navigationItem.leftBarButtonItem = backBar;
    
}

//返回主页
- (void)backTpMainView{
    
    MainPageViewController *mainVC = [[MainPageViewController alloc] init];
    
    [self.navigationController pushViewController:mainVC animated:nil];
    
    
}



@end
